/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   ColorConfig.cs
 * CreateData   :   2023/5/25 13:11:29
 * UnityVersion :   2021.3.20f1c1
 * Description  :   颜色配置
************************************************/

#if UNITY_EDITOR
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using UnityEditor;
#endif

using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using System.Linq;
using System;

namespace Main
{
    /// <summary>
    /// 颜色配置
    /// </summary>
    public class ColorConfig : ScriptableObjectConfigBase<ColorInfo>
    {
#if UNITY_EDITOR
        /// <summary>
        /// 生成 ID
        /// </summary>
        private string SpawnID
        {
            get
            {
                const int Length = 10000;
                const string name = "ZZ_White";
                for (int i = 0; i < Length; i++)
                {
                    string id = $"{name}_{i}";
                    if (!datas.ContainsKey(id))
                    {
                        return id;
                    }
                }
                throw new IndexOutOfRangeException("超出下标范围");
            }
        }
        [Button("添加"), HorizontalGroup("Button")]
        private void Add()
        {
            ColorInfo info = new ColorInfo() { color = Color.white, Hex = "FFFFFFFFF", RGBA = "(255.000, 255.000, 255.000, 255.000)" };
            datas.Add(SpawnID, info);
        }
        [Button("清空"), HorizontalGroup("Button")]
        private void Clear()
        {
            datas.Clear();
        }
#endif
    }
#if UNITY_EDITOR
    [InlineProperty(LabelWidth = 200)]
#endif
    public struct ColorInfo
    {
#if UNITY_EDITOR
        [HorizontalGroup, HideLabel, OnValueChanged("OnColorChange")]
#endif
        public Color color;
#if UNITY_EDITOR
        [HorizontalGroup, HideLabel, ReadOnly]
#endif
        public string Hex;
#if UNITY_EDITOR
        [HorizontalGroup, HideLabel, ReadOnly]
#endif
        public string RGBA;


        private void OnColorChange()
        {
            Hex = "#" + UnityEngine.ColorUtility.ToHtmlStringRGBA(color);
            RGBA = (color * 255).ToString().Substring(4);
        }
    }
}
